
#include <stdio.h>
#include <stdlib.h>

#define EXIT_SUCCES 0

void count(char c, int *cont_palabra, int *cont_byte, int *count_linea, int *anterior){
	*cont_byte += 1;
	if (c == '\n' || c == EOF)
		*count_linea += 1;
	if (*anterior == 0 && (c == ' ' || c == '\n' || c == EOF))
		*cont_palabra += 1;
	if (c == '\n' || c == ' ')
		*anterior = 1;
	else *anterior = 0;
}

void wc (FILE* f_input, int *cont_pal_tot, int *cont_byte_tot, int *cont_linea_tot) {

	int cont_palabra = 0;
	int cont_byte = 0;
	int cont_linea = 0;
	int anterior_fue_espacio_o_newline = 1; //0 si es false, 1 si es true
	char byte = (char)fgetc(f_input);
	while (!feof(f_input)){
		count (byte, &cont_palabra, &cont_byte, &cont_linea, &anterior_fue_espacio_o_newline);
		byte = fgetc(f_input);
	}
	*cont_pal_tot += cont_palabra;
	*cont_byte_tot += cont_byte;
	*cont_linea_tot += cont_linea;
	printf("palabras: %d, bytes: %d, lineas: %d\n", cont_palabra, cont_byte, cont_linea);
}

int main(int argc, char* argv[]) {
	FILE * fd =fopen("/home/juan/Taller/OrgaCompus/Orga/Debug/test.txt", "rb");
	int cont_palabra_total = 0;
	int cont_byte_total = 0;
	int cont_linea_total = 0;
    wc(fd, &cont_palabra_total, &cont_byte_total, &cont_linea_total);
    printf("Total: palabras: %d, bytes: %d, lineas: %d\n", cont_palabra_total,
    											  	  	 cont_byte_total,
    											  	  	 cont_linea_total);
    return EXIT_SUCCES;
}
